package com.ling.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ling.bean.Msg;
import com.ling.bean.User;
import com.ling.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class AdminUserController {
    private final UserService userService;

    @Autowired
    public AdminUserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "/adminUser", method = RequestMethod.GET)
    public String goToAdminUser() {
        return "/admin/adminUser";
    }

    //管理员查询所有用户,分页显示
    @RequestMapping(value = "/adminGetUsers/{pn}", method = RequestMethod.GET)
    @ResponseBody
    public Msg adminGetUsers(@Autowired HttpSession session, @PathVariable Integer pn) {
        //查看是否登录
        if (session.getAttribute("admin") == null) {
            return Msg.fail().add("error", "session失效，请重新登陆后重试！");
        }
        //已登录
        PageHelper.startPage(pn, 20);
        List<User> users = userService.getUsers();
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return Msg.success().add("pageInfo", pageInfo);
    }
}
